﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kovan.DAL.Config;
using Kovan.Model.Entity.Config;
using Kovan.Model.Parameter.Config;
using System.IO;
using Kovan.Library;
using Kovan.Library.IO;

namespace Kovan.BLL.Config
{
    public class BPicture
    {
        DPicture dal = new DPicture();

        #region 当前类的实例
        private static readonly BPicture Instances = new BPicture();

        private BPicture() { }

        public static BPicture GetInstances()
        {
            return Instances;
        }
        #endregion

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<tb_picture> GetList(PPicture param)
        {
            return dal.GetList(param);
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public tb_picture GetModel(long id)
        {
            return dal.GetModel(id);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(tb_picture model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(long id)
        {
            return dal.Delete(id);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(tb_picture model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int BatchDelete(IEnumerable<string> ids, string pgUkey)
        {
            string fileNames = string.Empty;
            var result = dal.BatchDelete(ids, out fileNames);
            if (!string.IsNullOrEmpty(fileNames))
            {
                var attr = fileNames.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (attr != null && attr.Length > 0)
                {
                    foreach (var item in attr)
                    {
                        var path = PathUtility.GetPhysicalPath(string.Format("~/content/Uploads/pic/{0}{1}", string.IsNullOrEmpty(pgUkey) ? "" : pgUkey + "/", item));
                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                    }
                }
            }
            return result;
        }
    }
}
